/* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
  //
// Copyright (c) 2006 Georgia Tech Research Corporation
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation;
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// Author: George F. Riley<riley@ece.gatech.edu>
//

// ns3 - On/Off Data Source Application class
// George F. Riley, Georgia Tech, Spring 2007
// Adapted from ApplicationFlow in GTNetS.

#include "flow-client-application.h"

NS_LOG_COMPONENT_DEFINE ("FlowClientApplication");

using namespace std;

namespace ns3 {

NS_OBJECT_ENSURE_REGISTERED (FlowClientApplication);

/*
TypeId
SingleDestFlowClient::GetTypeId (void)
{
  static TypeId tid = TypeId ("ns3::FlowClientApplication")
    .SetParent<Application> ()
    .AddConstructor<SingleDestFlowClient> ()
    .AddAttribute ("FlowBurst", "Maximum increment in TCP flows for every flow finished",
                   UintegerValue (5),
                   MakeUintegerAccessor (&SingleDestFlowClient::m_flowBurst),
                   MakeUintegerChecker<uint32_t>())
       ;
  return tid;
}
*/

  
TypeId
FlowClientApplication::GetTypeId (void)
{
  static TypeId tid = TypeId ("ns3::FlowClientApplication")
    .SetParent<Application> ()
    .AddConstructor<FlowClientApplication> ()
    .AddAttribute ("Remote", "The address of the destination",
                   AddressValue (),
                   MakeAddressAccessor (&FlowClientApplication::m_remote),
                   MakeAddressChecker ())
    .AddAttribute ("Local", "The Address on which to Bind the rx socket.",
                   AddressValue (),
                   MakeAddressAccessor (&FlowClientApplication::m_local),
                   MakeAddressChecker ())             
    .AddAttribute ("FlowBurst", "Maximum increment in TCP flows for every flow finished",
                   UintegerValue (5),
                   MakeUintegerAccessor (&FlowClientApplication::m_flowBurst),
                   MakeUintegerChecker<uint32_t>())
    .AddAttribute ("FlowMax", "Maximum num of concurrent TCP flows",
                   UintegerValue (0),
                   MakeUintegerAccessor (&FlowClientApplication::m_flowNum),
                   MakeUintegerChecker<uint32_t>())
    .AddAttribute ("FlowSize", "A RandomVariable which sets flow size in bytes",
                   RandomVariableValue (ConstantVariable (100000)),
                   MakeRandomVariableAccessor (&FlowClientApplication::m_flowSize),
                   MakeRandomVariableChecker ())                                
   .AddTraceSource ("UpdateFlowCount", "Update flow count.",
                  MakeTraceSourceAccessor (&FlowClientApplication::m_flowCountUpdate))
       ;
  return tid;
}

FlowClientApplication::FlowClientApplication () :
  m_flowCount(0)
{
  NS_LOG_FUNCTION_NOARGS ();
}

FlowClientApplication::~FlowClientApplication()
{
  NS_LOG_FUNCTION_NOARGS ();
}

void
FlowClientApplication::DoDispose (void)
{
  NS_LOG_FUNCTION_NOARGS ();

  // chain up
  Application::DoDispose ();
}

// Application Methods
void 
FlowClientApplication::StartApplication() // Called at time specified by Start
{
  NS_LOG_FUNCTION (this);
  StartFlows();
}    

void
FlowClientApplication::StartFlows()
{
  NS_LOG_FUNCTION (this << m_flowCount);
  for ( unsigned int i = 0; i < m_flowBurst && m_flowCount < m_flowNum; i++ )
  {
   StartFlow();
   m_flowCount++;
  }
  m_flowCountUpdate(m_flowCount);
}

void
FlowClientApplication::StartFlow()
{
  NS_LOG_FUNCTION (this);
  uint32_t size;
  do {
      size = (uint32_t)m_flowSize.GetValue();
  } while (!size );
   
  Ptr<FlowClient> flow = CreateObject<FlowClient> (GetNode());
  flow->SetFlowFinishCallback(MakeCallback(&FlowClientApplication::HandleFlowFinish, this));
  flow->Start(m_remote,size);
  m_flowList.push_back(flow);
}

void 
FlowClientApplication::HandleFlowFinish(Ptr<FlowClient> flow)
{
  NS_LOG_FUNCTION_NOARGS ();
  /*
  if (!m_statCallback.IsNull ())
  {
    m_statCallback ( flow->GetSize() , flow->GetDuration() );
  }
  */

  m_flowList.remove(flow);
  m_flowCount--;
  StartFlows();
}    

void 
FlowClientApplication::StopApplication() // Called at time specified by Stop
{
  NS_LOG_FUNCTION_NOARGS ();

  //shut sources down.
  while(!m_flowList.empty()) 
  {
    Ptr<FlowClient> flow = m_flowList.front(); 
    flow->Stop();
    m_flowList.pop_front();
  }
}

Address
FlowClientApplication::GetRemote() const
{
  return m_remote;
}

} // Namespace ns3
